<?php
/**
 * Unicode support functions 
 * @package tools
 * @author Mundusnovis studio
 * @version 1.0
 * @copyright 2007 Mundusnovis studio
 * @site http://mundusnovis.info
 */

/**
 * mbstr_initialize_prefix()
 * Initializes the prefix of multibyte extension 
 *
 * NOTE: This function at its initially state didn't only set the function prefix, it checked for the extensions and configured them.
 * @return string Name of extension
 */
function mbstr_initialize_prefix() {
	// Extensions priority: mbstring, iconv
	$exts = array('mbstring', 'iconv');
	// Setting the setup function prefix
	$f = 'setup_';
   
	$found = false;
   
	// Looping through the array to find the right extension
	for ($i=0; $i<count($exts); $i++) {
		// Check if a multibyte string support extension is loaded
		if (extension_loaded($exts[$i])) {
			// Configuring the extension
			$f .= $exts[$i];
			$f();
			$found = $exts[$i];
			break;
		} else {
			// Trying to load the extension
			if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
				$dl = @dl("php_{$exts[$i]}.dll");
			} else {
				$dl = @dl($exts[$i].'.so');
			}
			if ($dl) {
				// Configuring the extension
				$f .= $exts[$i];
				$f();
				$found = $exts[$i];
				break;
			}
		}
	}
   
	// If the extension is found, return the prefix, else show an error
	if ($found=='mbstring') return 'mb';
	else if ($found) return $found;
	else error('No multibyte string extension found.');
}

/**
 * setup_mbstring()
 * Configures the mbstring extension 
 */
function setup_mbstring() {
	mb_internal_encoding('UTF-8');
	mb_http_output('UTF-8');
}

/**
 * setup_iconv()
 * Configures the iconv extension 
 */
function setup_iconv() {
	iconv_set_encoding("internal_encoding", "UTF-8");
	iconv_set_encoding("output_encoding", "UTF-8");
}

/**
 * mbstr_strlen( string $str [, string $charset] ) 
 * Returns the character count of a multibyte string
 * @param string $str String to process
 * @param string $charset Encoding of given string
 * @return integer Length of given string
 */
function mbstr_strlen($str, $charset='UTF-8') {
	 $func = mbstr_initialize_prefix().'_strlen';
	 return $func($str, $charset);
}

/**
 * mbstr_strpos( string $haystack, string $needle [, int $offset [, string $charset]] )
 * Finds position of first occurrence of a needle within a haystack.
 * @param string $str String to process
 * @param string $charset Encoding of given string
 * @return integer Length of given string
 */
function mbstr_strpos($haystack, $needle, $offset=0, $charset='UTF-8') {
	 $func = mbstr_initialize_prefix().'_strpos';
	 return $func($haystack, $needle, $offset, $charset);
}

/**
 * mbstr_strrpos( string $haystack, string $needle [, int $offset [, string $charset]]  )
 * Finds the last occurrence of a needle within a haystack
 * @return int Substring position in given string
 */
function mbstr_strrpos($haystack, $needle, $offset=0, $charset='UTF-8') {
	// Setting the prefix
	$p = mbstr_initialize_prefix();
   
	$func = $p.'_strrpos';
	if ($p=='iconv') return $func($haystack, $needle, $charset);
	else return $func($haystack, $needle, $offset, $charset);
}

/**
 * mbstr_substr( string $str, int $offset [, int $length [, string $charset]] )
 * Cut out part of a multibyte string
 * @return string Cutted part of multibyte string
 */
function mbstr_substr($str, $offset, $length=0, $charset='UTF-8') {
 $func = mbstr_initialize_prefix().'_substr';
 return $func($haystack, $offset, $length, $charset);
}

/**
 * mbstr_convert( string $str [, string $to [, string $from]] )
 * Converts the character encoding to Unicode or other
 * @param string $str String to process
 * @param string $to Output encoding
 * @param string $to Optionally input encoding
 * @return string Converted string
 */
function mbstr_convert($str, $to='UTF-8', $from='') {
 $p = mbstr_initialize_prefix();
 
 if ($p=='iconv') return iconv($from, $to, $str);
 else return mb_convert_encoding($str, $to, $from);
}
?>